<?php
namespace App\Wen\Utils;


use App\Models\Pets\WxPetsAdoption;
use App\Models\Pets\WxPetsAdoptionApply;
use App\Models\Pets\WxPetsAdoptionCollect;
use Carbon\Carbon;

class PetUtils{

    // 年龄范围和具体年龄互转
    // { label: '请选择', value: 0 },
    //	  { label: '一个月左右', value: 1 },
    //	  { label: '两个月左右', value: 2 },
    //	  { label: '三个月左右', value: 3 },
    //	  { label: '四个月左右', value: 4 },
    //	  { label: '五个月左右', value: 5 },
    //	  { label: '六个月左右', value: 6 },
    //	  { label: '七个月左右', value: 7 },
    //	  { label: '八个月左右', value: 8 },
    //	  { label: '九个月左右', value: 9 },
    //	  { label: '1岁左右', value: 10 },
    //	  { label: '1岁半左右', value: 11 },
    //	  { label: '2岁左右', value: 12 },
    //	  { label: '2岁半左右', value: 13 },
    //	  { label: '3岁左右', value: 14 },
    //	  { label: '5岁左右', value: 15 },
    //	  { label: '10岁左右', value: 16 }],
    public static function age_range_transfer($range, $date = null){
        if($range === 0){
            return null;
        }
        $range = (int)$range;
        if(_empty_($date)){
            if($range < 10){
                return Carbon::today()->subMonths($range)->timestamp;
            }else if($range == 10){
                return Carbon::today()->subMonths(12)->timestamp;
            }else if($range == 11){
                return Carbon::today()->subMonths(18)->timestamp;
            }else if($range == 12){
                return Carbon::today()->subMonths(24)->timestamp;
            }else if($range == 13){
                return Carbon::today()->subMonths(30)->timestamp;
            }else if($range == 14){
                return Carbon::today()->subMonths(36)->timestamp;
            }else if($range == 15){
                return Carbon::today()->subMonths(60)->timestamp;
            }else if($range == 16){
                return Carbon::today()->subMonths(120)->timestamp;
            }
        }else{
            $today = date('Y-m-d'); // 当前日期
            $diff = date_diff(date_create($date), date_create($today));
            $years = $diff->format('%y');
            $months = $diff->format('%m');
            if($years >= 8){
                return 16;
            }else if($years >= 4){
                return 15;
            }else if($years >= 3){
                return 14;
            }else if($years >= 2){
                return 12;
            }else if($years >= 1){
                return 10;
            }else{
                if($months >= 9){
                    return 9;
                }else if($months >= 8){
                    return 8;
                }else if($months >= 7){
                    return 7;
                }else if($months >= 6){
                    return 6;
                }else if($months >= 5){
                    return 5;
                }else if($months >= 4){
                    return 4;
                }else if($months >= 3){
                    return 3;
                }else if($months >= 2){
                    return 2;
                }else if($months >= 1){
                    return 1;
                }else{
                    return 0;
                }

            }
        }
    }

    public static function adoption_apply_process($v, $uid = 0, $scene = 0){
        $v->marital_tip = PetUtils::marital_status()[$v->marital_status];
        $v->housing_tip = PetUtils::housing_status()[$v->housing_status];
        $v->working_tip = PetUtils::working_status()[$v->working_status];
        $v->birthday = explode(' ', $v->birthday)[0];
        $v->income_tip = PetUtils::income_types()[$v->income];
        $v->region = [$v->province, $v->city, $v->district];
        $v->region_tip = $v->province  . $v->city . $v->district;
        $v->status_tip  = self::apply_status()[$v->status];



        return $v;
    }

    public static function adoption_process($v, $uid = 0, $scene = 0){
        if($uid > 0){
            $v->myApplication = WxPetsAdoptionApply::where('pet_adoption_id', $v->id)->where('user_id', $uid)->whereIn('status', [0,1])->first();
        }

        if($v->myApplication){
            $v->myApplication->region = [$v->myApplication->province, $v->myApplication->city, $v->myApplication->district];
        }else{
            $v->myApplication = [
                'pet_adoption_id' => $v->id
            ];
        }
        $v->pictures = json_decode($v->pictures, true);
        $v->image = _array_key(_array_key($v->pictures, 0, ''), 'url', '');
        if($v->image){
            $v->image = $v->image . '?imageMogr2/thumbnail/200x/crop/200x200/gravity/center/format/webp/interlace/0';
        }
        $v->petAge = StrUtils::age($v->pet_age, 'Y-m-d H:i:s');
        $v->pet_age = explode(' ', $v->pet_age)[0];
        $v->age_range = PetUtils::age_range_transfer(null, $v->pet_age);

        $v->format_time = format_datetime($v->created_at);
        $adopt_status_0_count = WxPetsAdoption::where('user_id', $v->user_id)->where('adopt_status', 0)->count();
        $adopt_status_1_count = WxPetsAdoption::where('user_id', $v->user_id)->where('adopt_status', 1)->count();
        $user = UserUtils::get_cached_user($v->user_id, true, true);
        $user['adoption'] = [
            'count' => [
                'status_0' => $adopt_status_0_count,
                'status_1' => $adopt_status_1_count
            ],
        ];
        $v->user = $user;
        $v->cost_tip = $v->is_free == 0 ? '免费领养' : ( $v->is_free == 1 ? '有偿'.$v->cost.'元' : '押金'.$v->cost.'元' );
        $condition_tip = [];
        if($v->condition){
            $condition_arr = explode(',', $v->condition);
            $condition_ = [];
            if($condition_arr){
                foreach ($condition_arr as $item){
                    $condition_tip[] = PetUtils::adoption_condition()[$item];
                    $condition_[] = (int)$item;
                }
            }
            $v->condition = $condition_;
        }else{
            $v->condition = [];
        }
        $v->condition_tip =  $condition_tip;
        if($uid > 0){
            $v->is_collect = WxPetsAdoptionCollect::where('user_id', $uid)->where('pet_adoption_id', $v->id)->exists();
        }else{
            $v->is_collect = false;
        }
        $v->region = [$v->province, $v->city, $v->district];
        $v->collect_count = WxPetsAdoptionCollect::where('pet_adoption_id', $v->id)->count();

        $v->tags = [
            ['label'=>'绝育', 'isCheck'=>$v->is_sterilization, 'background'=>'#ff7a78'],
            ['label'=>'免疫', 'isCheck'=>$v->is_vaccine, 'background'=>'#6ecffc'],
            ['label'=>'驱虫', 'isCheck'=>$v->is_repellent, 'background'=>'#ffbd66'],
        ];

        return $v;
    }

    public static function apply_status(){
        return [
            0 => '申请中',
            1 => '申请成功',
            2 => '被拒绝'
        ];
    }

    public static function adoption_condition(){
        return [
            0 => '仅限同城',
            1 => '按时打疫苗',
            2 => '适龄绝育',
            3 => '遛狗拴绳，养猫封网',
            4 => '科学养宠，办理狗证',
            5 => '打卡反馈',
            6 => '签领养协议',
            7 => '全家同意',
            8 => '有稳定收入',
            9 => '不得遗弃、转让、贩卖、繁殖、虐待'
        ];
    }

    public static function marital_status(){
        return [
            0 => '单身',
            1 => '恋爱中',
            2 => '已婚',
        ];
    }

    public static function housing_status(){
        return [
            0 => '自住房',
            1 => '整租房',
            2 => '合租',
        ];
    }

    public static function working_status(){
        return [
            0 => '无业',
            1 => '学生',
            2 => '工作',
            3 => '退休',
        ];
    }

    public static function income_types(){
        return [
            0 => '小于3千',
            1 => '3千～6千',
            2 => '6千～1万',
            3 => '1万以上',
        ];
    }

}
